<?php
    session_start();
    require 'common/Utility.php';
    require 'db/configuration.php';
    require 'crud/CustomSecurity.class.php';
    require 'crud/GenProduct.class.php';
    require 'crud/Recepta_helper.class.php';
    require 'crud/Recepti.class.php';
    require 'crud/Categorii_recepti.class.php';
    require 'crud/Kolichestvo_map.class.php';
    require 'crud/Shoplist.class.php';
    require 'slist/slist.php';
    include_once("js/ajax/agent.php");
	loginCheck();
	loadDB();
    $agent->init();
    ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    <link rel="stylesheet" type="text/css" href="css/page.css" />
    <link rel="stylesheet" type="text/css" href="css/main.css" />
    <link rel="stylesheet" type="text/css" href="css/receptatest.css" />
    <script type="text/javascript" src="js/jquery-1.3.2.min.js"> </script>
    <script type="text/javascript" src="js/recepta.js"></script>
    <script type="text/javascript" src="js/slist.js"></script>
    <script type="text/javascript" src="js/textevents.js"></script>
    <script type="text/javascript" src="js/jedit/jquery.jeditable.mini.js"> </script>
	<title>Рецепта</title>
</head>
<body>	

<?php
initConvertor();
		
		$recaptaName = 'име на рецепта';
		$recaptaDescr = '';
		$recaptaCategory = '';
		$isAuthor = 1;
		$producti = array();
		if (isset($_GET['idr'])){
			$recepta = loadRecepta();
			$producti = loadProducti();
			$recaptaName = $recepta[0][1];
			$recaptaDescr = $recepta[0][2];
			$recaptaCategory = $recepta[0][3];
			if ($recepta[0][4] != $_SESSION['user_id']){
				$isAuthor = 0;
				echo "<script language=javascript> setReadOnly(); </script>";
			} else {
				echo '<input class="table-input" id="edit" type="button" value="редактиране" onclick="enableEdit();");"/>';
			}
		}
		if (isset($_POST['productsjs'])){
			saveRecepta();	
		}
		
?>
<script language="javascript" type="text/javascript" defer>
<!--
loadProducts(<?=json_encode($producti)?>);
// -->
</script>
<?php 
	if ( $isAuthor == 1){
		echo '<form action="recepta.php" method="post" name="saveform" onSubmit="return false;">';
	}
?>
	<div id="rp-body">
		<div class="rp-left">
			<div id="product-add">
			<?php
			if ( $isAuthor == 1){
				echo '<p>добави продукт:</p>';
			}
			 ?>
				<table>
				<?php
				if ($isAuthor == 1){
						echo '<tr>';
							echo '<td>';
								

							$quo = new GenProduct(con());
							$types = $quo->getAll_Plus_Koichestvo();
							echo '<select name=product id="combo" onchange="changeMerna();">';
							foreach($types as $element){
								echo '<option title="'.$element[1].'"; value="'. $element[0] .'">'. $element[0] .'</option>';
							}
							echo '</select>';
					
					
						echo '</td>';
						echo '<td>';
							echo '<input class="table-input" type="text" name="kolichestvo" id="qId" onkeypress="{if (event.keyCode==13)addProducts();}" />';
						echo '</td>';
						echo '<td>';
							echo '<div id="mernaEdinica"> </div>';
						echo '</td>';
						echo '<td>';
							echo '<input class="table-submit" type="button" value="+" onclick="addProducts();"/>';
						echo '</td>';
					echo '</tr>';
				}
					?>
				</table>
			</div>
			<div id="rp-produkti">
			</div>
			<div id="rp-dopylnitelni">
				<div>
					<ul>
						<li><a href="#">+ favorites</a></li>
						<li><a href="#" onclick="showHideShopList();">+ all products to list</a></li>
						<li><a href="#" onclick="showHideShopList();">+ missing products to list</a></li>
						<li><a href="#">email</a></li>
						<li><a href="#">print</a></li>
					</ul>
				</div>
			</div>
		</div>
		<div id="rpid-right" class="rp-right">
			<div id="rp-zaglawie">
				<div id="rp-zaglawie-noedit">
				<?php 
					echo $recaptaName;
				?>
				</div>
			</div>
			<div id="rp-zaglawie-edit">
			<?php
			if ( $isAuthor == 1){ 
				echo '<input class="table-input" id="rnameid" type="text" name="rName" value="'.$recaptaName.'" onfocus="putStringOnTextFocus(\'rnameid\',\'име на рецепта\');" onblur="putStringOnTextBlur(\'rnameid\',\'име на рецепта\');"/>';
			}
			?>
			</div>
			<div id="rp-kategoria">
				<div id="rp-kategoria-noedit">
				<?php echo $recaptaCategory;?>
				</div>
			</div>
			<div id="rp-kategoria-edit">
			<?php 
			if ( $isAuthor == 1){
						$categ = new Categorii_recepti(con());
							$categs = $categ->getAllNames();
							echo '<select name="category" id="categoryId">';
							
							foreach($categs as $option_categs){
								$selected = "";
								
								if (strcasecmp($recaptaCategory, $option_categs[0]) == 0){
									$selected = 'selected';
								} else {
									$selected = "";
								}
								echo '<option '.$selected.'>'. $option_categs[0] .'</option>';
								
							}
							echo '</select>';
			}
				?>
				
			</div>
			<div id="rp-ukazania">
				<div id="rp-ukazania-noedit">
				<?php 
					echo $recaptaDescr;
				?>
				</div>
			</div>
			<div id="rp-ukazania-edit">
			<?php
			if ( $isAuthor == 1){ 
					echo '<textarea class="table-input" name="description" id="description">'.$recaptaDescr.' </textarea>';
					echo '<input type="hidden" name="productsjs"/>';
					echo '<input type="hidden" name="idr" value='.getId().'/>';
					echo '<input class="table-submit" id="save" type="button" value="запиши промените" onclick="submitRecepta();"/>';
			}
				?>
			</div>
		
	</div>
</div>

	<?php
			function getId(){
				if(isset($_GET['idr'])){
					return $_GET['idr'];
				}
				return -1;
			}
			function saveProducts($id){
				$crud = new Recepta_helper(con());
				$productsTemp = explode(',',$_POST['productsjs']);
				if ($_POST['idr'] > 0 ){
					$crud->remove($id);
				}
				for ($i = 0; $i+1 < count($productsTemp); $i++) {
					$crud->add($id, $productsTemp[$i], $productsTemp[$i+1], $productsTemp[$i+2]);
					$i=$i+2;
				}
				
			} 

			function saveRecepta(){
				if(strlen($_POST['rName']) == 0 || strlen($_POST['description']) == 0){
					return;
				}
				$receptaCrud = new Recepti(con());
				if ($_POST['idr'] >0 ){
					$result = $receptaCrud->update($_POST['idr'], $_POST['rName'], $_POST['description'], $_POST['category']);
				} else {
					$result = $receptaCrud->add($_POST['rName'], $_POST['description'], $_POST['category']);	
				}
				
				if ($result != -1){
					saveProducts($result);
				}
				echo "<script language=javascript> parent.location.reload(1); </script>";
			}
			
			function loadRecepta(){
				$receptaCrud = new Recepti(con());
				return $receptaCrud->getObject($_GET['idr']);
			}
			
			function loadProducti(){
				$crud = new Recepta_helper(con());
				return $crud->getObject($_GET['idr']);
				
			} 
			
			if ($isAuthor == 1){
				echo '</form>';	
			}
			
			?>
			
<?php 
function initConvertor(){
	$crud = new Kolichestvo_map(con());
	echo '<script language="javascript" type="text/javascript" defer> setMap('.json_encode($crud->getAll()).');</script>';
}			
?>
<?php
if (!isset($_GET['idr'])){
	echo "<script language=javascript> makeItEditable(); </script>";
} 
?>
<script language="javascript" type="text/javascript" defer>
<!--
listTable();
changeMerna();
// -->
</script>
</body>
</html>	
	